package com.example.mobile2021606129wje0401;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

//d点击开灯按钮，显示明亮图片；点击关灯，显示黑暗图片
public class MainActivity extends AppCompatActivity {

    private ImageView lightOff;
    private Button btnOpen;
    private Button btnClose;
    private Button btnFan;

    private boolean fanStatus=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lightOff.setImageResource(R.drawable.light_on);
            }
        });
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lightOff.setImageResource(R.drawable.light_off);
            }
        });
        btnFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fanStatus) {
                    lightOff.setImageResource(R.drawable.fan_off);
                    fanStatus = false;
                } else {
                    lightOff.setImageResource(R.drawable.fan_on);
                    fanStatus = true;
                }
            }
        });
    }
    private void initView() {
        lightOff = (ImageView) findViewById(R.id.light_off);
        btnOpen = (Button) findViewById(R.id.btn_open);
        btnClose = (Button) findViewById(R.id.btn_close);
        btnFan = (Button) findViewById(R.id.btn_fan);
       }
    }
